﻿using System;
using System.Collections.Generic;

namespace Halation.Extensions
{
    /// <summary>
    /// 字典接口扩展函数
    /// </summary>
    public static class IDictionaryEx
    {
        /// <summary>
        /// 获取字典的值
        /// </summary>
        /// <typeparam name="Key">键类型</typeparam>
        /// <typeparam name="Value">值类型</typeparam>
        /// <param name="dict">字典</param>
        /// <param name="key">键</param>
        /// <param name="handler">如果发现值之后处理</param>
        public static void GetValue<Key, Value>(this IDictionary<Key, Value> dict, Key key, Action<Value> handler)
        {
            if (dict == null || handler == null) return;
            Value value;
            if (dict.TryGetValue(key, out value)) handler(value);
        }
        /// <summary>
        /// 获取字典的值
        /// </summary>
        /// <typeparam name="Key">键类型</typeparam>
        /// <typeparam name="Value">值类型</typeparam>
        /// <param name="dict">字典</param>
        /// <param name="key">键</param>
        /// <param name="handler">如果发现值之后处理</param>
        /// <param name="defaultValue">默认值</param>
        public static void GetValue<Key, Value>(this IDictionary<Key, Value> dict, Key key, Action<Value> handler, Value defaultValue)
        {
            if (handler == null) return;
            if (dict == null)
            {
                handler(defaultValue);
                return;
            }
            Value value;
            if (dict.TryGetValue(key, out value)) handler(value);
            else handler(value);
        }
        /// <summary>
        /// 获取字典的值
        /// </summary>
        /// <typeparam name="Key">键类型</typeparam>
        /// <typeparam name="Value">值类型</typeparam>
        /// <param name="dict">字典</param>
        /// <param name="key">键</param>
        /// <param name="defaultValue">默认值</param>
        public static Value GetValue<Key, Value>(this IDictionary<Key, Value> dict, Key key, Value defaultValue = default(Value))
        {
            if (dict == null) return defaultValue;
            Value value;
            if (dict.TryGetValue(key, out value)) return value;
            else return defaultValue;
        }
        /// <summary>
        /// 获取字典的值
        /// </summary>
        /// <typeparam name="Key">键类型</typeparam>
        /// <typeparam name="Value">值类型</typeparam>
        /// <param name="dict">字典</param>
        /// <param name="key">键</param>
        /// <param name="create">默认值</param>
        public static Value GetValue<Key, Value>(this IDictionary<Key, Value> dict, Key key, Func<Key, Value> create)
        {
            if (dict == null) throw new NullReferenceException("dict");
            Value value;
            if (dict.TryGetValue(key, out value)) return value;
            else if (create == null) return default(Value);
            {
                value = create(key);
                dict[key] = value;
                return value;
            }
        }
    }
}
